package com.airnewslinkosky.ticket.ui.flight;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.Contactor;
import com.airnewslinkosky.ticket.model.FlightInfo;
import com.airnewslinkosky.ticket.model.SearchFlight;
import com.airnewslinkosky.ticket.pay.util.BaseHelper;
import com.airnewslinkosky.ticket.sql.FavoriteInfoDB;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.mobclick.android.MobclickAgent;

public class ContactorAdd extends BaseActivity implements
		AdapterView.OnItemClickListener {

	public  final int DIALOG_IDTYPE_LIST = 10;
	public  final int DATE_DIALOG_ID = 9;
	
	public static SearchFlight searchFlight;
	private Button backBut;
	
	public static FlightInfo flight;//去程
	public static FlightInfo returnFlight;//回程
	public static List<Map<String,String>> passengerList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> contactList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> itineraryList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> mileagerInfoList = new ArrayList<Map<String,String>>();
	
	public SimpleAdapter sa;
	public ListView lv;
	public Button mBtnBack,mBtnNext,contactorSaveBut;
	List<Map<String, String>> data = new ArrayList<Map<String, String>>();

	private ProgressDialog progressDialog = null;
	//证件类型,生日
	private TextView idType,birthday;
	private LinearLayout idTypeLay,birthdayLay;
	
	private int postion=0;
	private int mYear;
    private int mMonth;
    private int mDay;
    
    private String sex = "0";
    
    //联系人,电话
    private EditText contactorName,contactorMobile,email;

    private Toast  toast ;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fly_contactor_info);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
		activityList.add(this);
		
		//得到当前日期
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        
        backBut = (Button)findViewById(R.id.backBut);
        
//		birthdayLay = (LinearLayout)findViewById(R.id.birthdayLay);
//		birthday =  (TextView)findViewById(R.id.birthday);
		
		contactorName = (EditText)findViewById(R.id.contactorName);
		contactorMobile = (EditText)findViewById(R.id.contactorMobile);
		email = (EditText)findViewById(R.id.email);
		
		contactorSaveBut = (Button)findViewById(R.id.contactorSaveBut);
        
		OnClickListener b_ocl= new OnClickListener() { 
	    	public void onClick(View v) { 
	    		switch (v.getId()) {
		    		case R.id.idTypeLay:
		    			showDialog(DIALOG_IDTYPE_LIST);
		    			break;
//		    		case R.id.birthdayLay:
//		    			showDialog(DATE_DIALOG_ID);
//		    			break;
		    		case R.id.contactorSaveBut:
		    			if ("".equals(contactorName.getText().toString()) || "".equals(contactorMobile.getText().toString()) || "".equals(email.getText().toString())){
//		    				Toast.makeText(ContactorAdd.this, "请填写完整信息", Toast.LENGTH_LONG).show();
		    				BaseHelper.showDialog(ContactorAdd.this, "提示", "请填写完整信息",
		    						R.drawable.infoicon);
		    				return;
		    			}
		    			String name = contactorName.getText().toString();
		    			if(!name.matches("(^[\u4e00-\u9fa5]{2,6}$)|(^[a-zA-z][a-zA-z\\s]{0,18}[a-zA-z]$)")){
		    				toast = Toast.makeText(ContactorAdd.this, "姓名应为2-6个中文字符或2-20个英文", Toast.LENGTH_LONG);
							toast.setGravity(Gravity.CENTER, 0, 0);
							toast.show();
		    				return;				
		    			}
						if (!contactorMobile.getText().toString().matches("[0-9]{11}")) {
							toast = Toast.makeText(ContactorAdd.this, "手机号填写不正确", Toast.LENGTH_LONG);
							toast.setGravity(Gravity.CENTER, 0, 0);
							toast.show();
							return;
						}
						if (!email.getText().toString().matches("\\w+@(\\w+\\.)+[a-z]{2,3}")) {
							toast = Toast.makeText(ContactorAdd.this, "Email填写不正确", Toast.LENGTH_LONG);
							toast.setGravity(Gravity.CENTER, 0, 0);
							toast.show();
							return;
						}

		    			HashMap<String, String> map = new HashMap<String, String>();
		    			map.put("contactName", contactorName.getText().toString());
		    			map.put("phone", contactorMobile.getText().toString());
		    			map.put("email", email.getText().toString());
		    			map.put("birthday", "");
		    			map.put("sex", sex);
		    			data.add(map);
		    			
		    			Contactor contactor = new Contactor();
		    			contactor.setContactName(contactorName.getText().toString());
		    			contactor.setContactNO("");
		    			contactor.setSex(sex);
		    			contactor.setBirthday("");
		    			contactor.setEmail(email.getText().toString());
		    			contactor.setPhone(contactorMobile.getText().toString());
		    			FavoriteInfoDB.setContactor(ContactorAdd.this, contactor);
		    			
		    			FlyOrderInput.contactList = data;
		    			
		    			Intent intent = new Intent();
						intent.setClass(ContactorAdd.this, FlyOrderInput.class);
						FlyOrderInput.flight = flight;
						FlyOrderInput.returnFlight = returnFlight;
						FlyOrderInput.searchFlight = searchFlight;
						startActivity(intent);
		    			break;
		    		case R.id.backBut:
		    			Intent intent3 = new Intent();
		    			intent3.setClass(ContactorAdd.this, FlyOrderInput.class);
		    			FlyOrderInput.flight = flight;
						FlyOrderInput.returnFlight = returnFlight;
						FlyOrderInput.searchFlight = searchFlight;
						FlyOrderInput.passengerList = passengerList;
						FlyOrderInput.mileagerInfoList = mileagerInfoList;
						FlyOrderInput.itineraryList = itineraryList;
		    			startActivity(intent3);
		    			break;		
	    		}
	    	}
		};
		
//		birthdayLay.setOnClickListener(b_ocl);
		backBut.setOnClickListener(b_ocl);
		contactorSaveBut.setOnClickListener(b_ocl);
	    
//		RadioGroup radioGroup = (RadioGroup) findViewById(R.id.sexgroup);
//		radioGroup.check(R.id.manRadioButton);
//		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                RadioButton radioButton = (RadioButton) findViewById(checkedId);
//                sex = String.valueOf(radioButton.getText());
//                if("男".equals(sex)) {
//                	sex = "1";
//                } else if("女".equals(sex)) {
//                	sex = "0";
//                } 
//            }
//        });
		
	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		    case DIALOG_IDTYPE_LIST:
	            return new AlertDialog.Builder(ContactorAdd.this)
	                .setIcon(R.drawable.ic_menu_more)
	                .setTitle("请选择")
	                .setSingleChoiceItems(R.array.paperTypes, 0, new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    	postion = whichButton;
	                    	String[] arrs = getResources().getStringArray(R.array.paperTypes);
	                    	idType.setText(arrs[whichButton]);
	                    	dialog.dismiss();
	                        /* User clicked on a radio button do some stuff */
	                    }
	                })
	                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    
	                    }
	                })
	                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    }
	                })
	               .create();
		    case DATE_DIALOG_ID:
		    	return new DatePickerDialog(ContactorAdd.this, dsl, mYear, mMonth, mDay);
		    default:
				return null;
		}
	}
	
    //准备日期设置监听器
    private OnDateSetListener dsl = new DatePickerDialog.OnDateSetListener(){

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear =year;
			mMonth= monthOfYear;
			mDay= dayOfMonth;
			birthday.setText(mYear+"-"+(mMonth+1)+"-"+mDay);
		}
    };

	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
	
}
